package com.stud.system.controller.workflow;

import com.stud.common.core.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.core.page.TableDataInfo;
import com.stud.common.enums.FileBusinessType;
import com.stud.system.domain.WorkflowTask;
import com.stud.system.service.FileLinkService;
import com.stud.system.service.WorkflowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 流程实例任务Controller
 *
 * @author Stud
 * @date 2023-03-06
 */
@Api(tags = "流程实例任务")
@RestController
@RequestMapping("/system/workflowTask")
@RequiredArgsConstructor
public class WorkflowTaskController extends BaseController {

    private final WorkflowTaskService workflowTaskService;

    private final FileLinkService fileLinkService;

    @ApiOperation("查询流程实例任务列表")
    @PreAuthorize("@ss.hasPermi('system:workflowTask:list')")
    @GetMapping("/list")
    public TableDataInfo<WorkflowTask> list(@RequestParam Long workflowId) {
        List<WorkflowTask> list = workflowTaskService.listByWorkflowId(workflowId);

        List<Long> idList = list.stream().map(WorkflowTask::getId).collect(Collectors.toList());
        Map<Long, List<Long>> fileIdMap = fileLinkService.fileLinkIdMap(FileBusinessType.WORKFLOW_TASK, idList);
        list.forEach(temp -> {
            if (fileIdMap.containsKey(temp.getId())) {
                temp.setFileLinkIdList(fileIdMap.get(temp.getId()));
            }
        });

        return getDataTable(list);
    }

    @ApiOperation("获取流程实例任务详细信息")
    @PreAuthorize("@ss.hasPermi('system:workflowTask:query')")
    @GetMapping(value = "/{id}")
    public Result<WorkflowTask> getInfo(@PathVariable("id") Long id) {
        return Result.ok(workflowTaskService.getById(id));
    }

}
